﻿using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using COC.Commons;
using COC.Webs;

namespace COC.Tool
{
    public partial class Workbench : Form
    {
        private readonly GameConfig _GameConfig = new GameConfig();
        private readonly PictureController _PictureController = new PictureController();
        private readonly WebMonitor _WebMonitor = new WebMonitor();
        private readonly ItemPictureManager _ItemPictureManager = new ItemPictureManager();
        private readonly CodingPlanView _CodeingPlanView = new CodingPlanView();
        private Dictionary<string, Image> _PictureHeadMap = new Dictionary<string, Image>();

        public Workbench()
        {
            InitializeComponent();
            ControlEventManager();
            UpdateTreeView();
        }

        private void UpdateTreeView()
        {
            //初始化treeview目录树
            var rootNode = new TreeNode
            {
                Text = "部落冲突",
                Name = "COC_GameConfig",
                ImageKey = "COC_GameConfig.png",
                SelectedImageKey = "COC_GameConfig.png"
            };
            _MainTreeView.Nodes.Add(rootNode);

            var xpath = "/COC_GameConfig";
            var iocnPath = "picture/treeViewIcon";
            var headPath = "picture/head";

            //填充_iconImageList
            FillImageList(iocnPath);
            //取头像字典
            _PictureHeadMap = _PictureController.GetImageList(headPath);

            //获取xml的根节点的子节点
            var map = _GameConfig.GetNodePropertiesMap(xpath);
            //渲染一级目录
            FillTreeNode(rootNode, map);
            //渲染第二级以后的目录
            foreach (TreeNode node in rootNode.Nodes)
                FillChildNodes(node, $"{xpath}/{node.Name}");
            _MainTreeView.ExpandAll();
        }

        /// <summary>
        ///     给treeview填充ImageList当作图标库
        /// </summary>
        /// <param name="iconPath"></param>
        private void FillImageList(string iconPath)
        {
            //获取图片形成一个集合
            var iconMap = _PictureController.GetImageList(iconPath);
            foreach (var icon in iconMap)
                _iconImageList.Images.Add(icon.Key, icon.Value);
        }

        private void FillChildNodes(TreeNode node, string xpath)
        {
            //获取上面路径的节点成为一个字典
            var nextMap = _GameConfig.GetNodePropertiesMap(xpath);
            //将字典渲染到刚刚定位的treeview节点中
            FillTreeNode(node, nextMap);
            foreach (TreeNode subNode in node.Nodes)
            {
                var subXpath = $"{xpath}/{subNode.Name}";
                if (_GameConfig.HasChildNodes(subXpath))
                    FillChildNodes(subNode, subXpath);
            }
        }

        /// <summary>
        ///     将字典内容插入到treeview中指定层的指定路径中
        /// </summary>
        /// <param name="node">要插入的treeview目录树</param>
        /// <param name="map">要插入的字典</param>
        private static void FillTreeNode(TreeNode node, Dictionary<string, string> map)
        {
            if (map.Count <= 0) return;
            foreach (var data in map)
            {
                if (string.IsNullOrEmpty(data.Value)) continue;
                var newNode = new TreeNode
                {
                    Name = data.Key,
                    Text = data.Value
                };
                newNode.ImageKey = $"{newNode.Name}{".png"}";
                newNode.SelectedImageKey = $"{newNode.Name}{".png"}";
                node.Nodes.Add(newNode);
            }
        }

        /// <summary>
        ///     事件
        /// </summary>
        private void ControlEventManager()
        {
            _UpdateToolStripMenuItem.Click += (s, e) =>
            {
                if (_WebMonitor.UpdateData())
                {
                    //当官网有游戏数据更新时
                }
            };
            _MainTreeView.NodeMouseClick += (s, e) =>
            {
                if(e.Button == MouseButtons.Left)
                {
                    _PictureHead.Image = _PictureHeadMap.ContainsKey($"{e.Node.Name}{".jpg"}") ? _PictureHeadMap[$"{e.Node.Name}{".jpg"}"] : null;
                    GameData gb = new GameData();
                    var title = gb.GetItemTitle(e.Node.Name);
                    var data = gb.GetItemData(title,e.Node.Name);
                    UpdataDataListView(title,data);
                    textBox3.Text = $"{e.Node.Text}:{e.Node.Name}";
                }

                if (e.Button == MouseButtons.Right)
                {
                    _MainTreeView.SelectedNode = _MainTreeView.GetNodeAt(e.X, e.Y);
                    textBox3.Text = e.Node.Text;
                    _PictureHead.Image = _PictureHeadMap.ContainsKey($"{e.Node.Name}.jpg") ? _PictureHeadMap[$"{e.Node.Name}.jpg"] : null;
                }
            };
        }

        private void UpdataDataListView(Dictionary<string, string> title,List<Dictionary<string, string>> data)
        {
            this.DataView.Clear();
            this.DataView.View = System.Windows.Forms.View.Details;

            DataView.Columns.Add("",0);

            foreach (var col in title)
            {
                ColumnHeader ch = new ColumnHeader();
                ch.Name = col.Key;
                ch.Text = col.Value;
                ch.Width = this.DataView.Width / title.Count;
                ch.TextAlign = HorizontalAlignment.Center;
                this.DataView.Columns.Add(ch);
            }
            foreach (var cells in data)
            {
                ListViewItem item = new ListViewItem();
                foreach (var cell in cells)
                {
                    item.SubItems.Add(cell.Value);
                }
                DataView.Items.Add(item);
            }
        }

        private void 更换图片ToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            _ItemPictureManager.ShowDialog();
        }

        private void 研发计划ToolStripMenuItem_Click(object sender, System.EventArgs e)
        {
            _CodeingPlanView.ShowDialog();
        }
    }
}